/**
 * 基础配置类
 */
//加载菜单树
$(function () {
    //加載首頁面
    $('#content').load("../tpl/base/home.html");

    //加載資源菜單
    $.ajax({
        url: wwwroot + 'base/resource',
        method: 'GET',
        data: {
            sortJsonArray: encode64(utf16to8(JSON.stringify([]))),
            conditionJsonArray: encode64(utf16to8(JSON.stringify([]))),
            pageNum: '',
            pageSize: ''
        },
        success: function (data) {
            var menuDataTree = data.dataObj.rows.toTree(
                {
                    parentField: 'resParentResourceId',
                    textField: 'resName',
                    idField: 'id',
                    childrenField: 'children'
                }
            );
            if (menuDataTree && menuDataTree.length > 0) {
                for (var i = 0; i < menuDataTree.length; i++) {
                    if (menuDataTree[i].isShow == '1' && menuDataTree[i].resType == 'system') {
                        var platforms = menuDataTree[i].children;
                        for (var j = 0; j < platforms.length; j++) {
                            if (platforms[j].isShow == '1' && platforms[j].resType == 'platform') {
                                $('#headerMenu').append('<li style="float:right;" class="headerNav" key="' +
                                    platforms[j].id +
                                    '"><a href="#" class="text"><i class="' +
                                    platforms[j].resIcon +
                                    '"></i>' +
                                    platforms[j].resName
                                    + '</a></li>');
                            }
                        }
                    }
                }
                sidebar(menuDataTree[0].children[menuDataTree[0].children.length-1]);
                $('.headerNav').click(function () {
                    var id = $(this).attr('key');
                    var leftCatlog = menuDataTree[0].children.getBykey(id, 'id');

                    console.log(leftCatlog);
                    //首頁
                    $('#content').load(leftCatlog.resUrl);
                    sidebar(leftCatlog);
                });
            }
        }
    });
});

//菜单点击
function menuClick() {
    pagechange();
    $('.submenu > a').click(function (e) {
        e.preventDefault();
        var submenu = $(this).siblings('ul');
        var li = $(this).parents('li');
        var submenus = $('#sidebar li.submenu ul');
        var submenus_parents = $('#sidebar li.submenu');
        if (li.hasClass('open')) {
            if (($(window).width() > 768) || ($(window).width() < 479)) {
                submenu.slideUp();
            } else {
                submenu.fadeOut(250);
            }
            li.removeClass('open');
        } else {
            if (($(window).width() > 768 || ($(window).width() < 479))) {
                submenus.slideUp();
                submenu.slideDown();
            } else {
                submenus.fadeOut(250);
                submenu.fadeIn(250);
            }
            submenus_parents.removeClass('open');
            li.addClass('open');
        }
    });
    var ul = $('#sidebar > ul');
    $('#sidebar > a').click(function (e) {
        e.preventDefault();
        var sidebar = $('#sidebar');
        if (sidebar.hasClass('open')) {
            sidebar.removeClass('open');
            ul.slideUp(250);
        } else {
            sidebar.addClass('open');
            ul.slideDown(250);
        }
    });

    //Resize window related
    $(window).resize(function () {
        if ($(window).width() > 479) {
            ul.css({display: 'block'});
            $('#content-header .btn-group').css({width: 'auto'});
        }
        if ($(window).width() < 479) {
            ul.css({display: 'none'});
            fix_position();
        }
        if ($(window).width() > 768) {
            $('#user-nav > ul').css({width: 'auto', margin: '0'});
            $('#content-header .btn-group').css({width: 'auto'});
        }
    });

    if ($(window).width() < 468) {
        ul.css({display: 'none'});
        fix_position();
    }
    if ($(window).width() > 479) {
        $('#content-header .btn-group').css({width: 'auto'});
        ul.css({display: 'block'});
    }
    // === Tooltips === //
    /*$('.tip').tooltip();
     //$('.tip').tooltip();
     // $('.tip-left').tooltip({ placement: 'left' });
     // $('.tip-right').tooltip({ placement: 'right' });
     // $('.tip-top').tooltip({ placement: 'top' });
     // $('.tip-bottom').tooltip({ placement: 'bottom' });*/

    // === Search input typeahead === //
    /*$('#search input[type=text]').typeahead({
     source: ['Dashboard','Form elements','Common Elements','Validation','Wizard','Buttons','Icons','Interface elements','Support','Calendar','Gallery','Reports','Charts','Graphs','Widgets'],
     items: 4
     });*/

    // === Fixes the position of buttons group in content header and top user navigation === //
    function fix_position() {
        var uwidth = $('#user-nav > ul').width();
        $('#user-nav > ul').css({width: uwidth, 'margin-left': '-' + uwidth / 2 + 'px'});

        var cwidth = $('#content-header .btn-group').width();
        $('#content-header .btn-group').css({width: cwidth, 'margin-left': '-' + uwidth / 2 + 'px'});
    }

    // === Style switcher === //
    $('#style-switcher i').click(function () {
        if ($(this).hasClass('open')) {
            $(this).parent().animate({marginRight: '-=190'});
            $(this).removeClass('open');
        } else {
            $(this).parent().animate({marginRight: '+=190'});
            $(this).addClass('open');
        }
        $(this).toggleClass('icon-arrow-left');
        $(this).toggleClass('icon-arrow-right');
    });

    $('#style-switcher a').click(function () {
        var style = $(this).attr('href').replace('#', '');
        $('.skin-color').attr('href', 'css/maruti.' + style + '.css');
        $(this).siblings('a').css({'border-color': 'transparent'});
        $(this).css({'border-color': '#aaaaaa'});
    });

    $('.lightbox_trigger').click(function (e) {

        e.preventDefault();

        var image_href = $(this).attr("href");

        if ($('#lightbox').length > 0) {

            $('#imgbox').html('<img src="' + image_href + '" /><p><i class="icon-remove icon-white"></i></p>');

            $('#lightbox').slideDown(500);
        }

        else {
            var lightbox =
                '<div id="lightbox" style="display:none;">' +
                '<div id="imgbox"><img src="' + image_href + '" />' +
                '<p><i class="icon-remove icon-white"></i></p>' +
                '</div>' +
                '</div>';

            $('body').append(lightbox);
            $('#lightbox').slideDown(500);
        }

    });


    /* $('#lightbox').live('click', function() {
     $('#lightbox').hide(200);
     });*/

}

//页面尺寸算法
function pagechange() {
    //content自适应页面大小
    var pageHeight = $(window).height();
    var pageTopHeight = $('#header').height();
    var contentHeight = pageHeight - pageTopHeight;
    $('#content').css('height', contentHeight);
    $(window).resize(function () {
        var pageHeight = $(window).height();
        var pageTopHeight = $('#header').height();
        var contentHeight = pageHeight - pageTopHeight;
        $('#content').css('height', contentHeight);
    });

    //鼠标点击一级菜单
    $('.submenu').click(function () {
        $(this).css('background-color', '#08c');
        $(this).siblings().not($(this)).css("background-color", "#2E363F");
        $(this).addClass('active');
        $('.submenu').siblings().not($(this)).removeClass('active');
    });
    //鼠标点击二级菜单
    $('.submenu ul li').click(function () {
        /*$('ul > li').click(function(){
         $('li').css('background-color', 'green');
         $('li').not($(this)).css('background-color', 'red');*/
        $(this).css("background-color", "#28b779");
        $(this).siblings().not($(this)).css("background-color", "");
    });
    $('#content').css('overflow-y', 'auto');
}

//左側菜單傳入數值
function sidebar(leftCatlog) {
    //noinspection JSValidateTypes
    $('#sidebar ul').children().remove();

    var leftMenu = leftCatlog.children;
    if (leftMenu && leftMenu.length > 0) {
        for (var k = 0; k < leftMenu.length; k++) {
            $('#sidebarMenu').append('<li class="active submenu"><a href="#"><i class="' +
                leftMenu[k].resIcon
                + '"></i>' +
                leftMenu[k].resName
                + '</a><ul id="' +
                leftMenu[k].id
                + '"></ul></li>');
            var subMenu = leftMenu[k].children;
            if (subMenu && subMenu.length > 0) {
                for (var j = 0; j < subMenu.length; j++) {
                    $('#' + leftMenu[k].id).append('<li url="'+subMenu[j].resUrl+'"><a><i class="' +
                        subMenu[j].resIcon
                        + '"></i>' +
                        subMenu[j].resName
                        + '</a></li>');
                }
            }
        }

        $('#sidebarMenu li').css('background-color', '#2e363f');
        menuClick();
    }

    //鼠标点击一级菜单
    $('.submenu').click(function () {
        $(this).addClass('active');
        $('.submenu').siblings().not($(this)).removeClass('active');
    });
    //鼠标点击二级菜单
    $('.submenu ul li').click(function () {
        /*$('ul > li').click(function(){
         $('li').css('background-color', 'green');
         $('li').not($(this)).css('background-color', 'red');*/
        $(this).css("background-color", "#28b779");
        $(this).siblings().not($(this)).css("background-color", "");
        var url = $(this).attr('url');
        $('#content').load(url);
    });
}
